package com.woniu.sharepark.platformsystem.log.controller;


import com.github.pagehelper.PageInfo;
import com.woniu.sharepark.platformsystem.log.annonation.BackgroundLogAnnotation;
import com.woniu.sharepark.platformsystem.log.pojo.Log;
import com.woniu.sharepark.platformsystem.log.service.LogService;
import com.woniu.sharepark.platformsystem.log.util.ResultUtil;
import com.woniu.sharepark.platformsystem.util.ParamsUtil;
import com.woniu.sharepark.vo.LayuiDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zy
 * @date 2019/6/3 19:38
 */
@RequestMapping("/log")
@Controller
public class LogController {

    @Autowired
    private LogService logService;

    /**
     * 展示日志，可根据操作动态查询
     * @param loOperate
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public LayuiDataVO logList(@RequestParam(value = "page", required = false, defaultValue = "1")Integer currentPage,
                               @RequestParam(value = "limit", required = false, defaultValue = "10")Integer pageSize,
                               @RequestParam(value = "loOperate", required = false)String loOperate){
        List<Log> logs = logService.selectLogsByCondition(loOperate,currentPage,pageSize);
        PageInfo<Log> pageInfo = new PageInfo<>(logs);



        return  new LayuiDataVO(0,"ok",pageInfo.getTotal(),logs);
    }

    /**
     * 根据主键删除日志
     * @param
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @BackgroundLogAnnotation(operate = "删除日志")
    public ResultUtil deleteLogs(String params){
        Integer[] ids = ParamsUtil.stringToIntArray(params);

        try {
            int result = logService.deleteLogs(ids);
            return ResultUtil.ok("成功删除"+result+"条数据");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除失败");
        }
    }

}
